<?php
/*
Plugin Name: WP-PageNavi
Plugin URI: http://lesterchan.net/portfolio/programming/php/
Description: Adds a more advanced paging navigation to your WordPress blog.
Version: 2.40
Author: Lester 'GaMerZ' Chan
Author URI: http://lesterchan.net
*/

function wp_pagenavi_lite($totalpage=1) {
	if (!is_single()) {
		$pagenavi_options = array();
		$pagenavi_options['num_pages'] = 3; //
		
		$curpage = intval(get_query_var('paged'));//
		if(empty($curpage) || $curpage == 0) {
			$curpage = 1;
		}
		
		$pages_to_show_minus_1 = $pagenavi_options['num_pages']-1;
		$half_page_start = floor($pages_to_show_minus_1/2);
		$half_page_end = ceil($pages_to_show_minus_1/2);
		$start_page = $curpage - $half_page_start;
		if($start_page <= 0) {
			$start_page = 1;
		}
		$end_page = $curpage + $half_page_end;
		if(($end_page - $start_page) != $pages_to_show_minus_1) {
			$end_page = $start_page + $pages_to_show_minus_1;
		}
		if($end_page > $totalpage) {
			$start_page = $totalpage - $pages_to_show_minus_1;
			$end_page = $totalpage;
		}
		if($start_page <= 0) {
			$start_page = 1;
		}
		if($totalpage > 1) {
			echo '<div class="pager">'."\n";
			if($curpage>1) echo  '<a href="'.esc_url(get_pagenum_link($curpage-1)).'" class="pre">上页</a>';
			
			if ($start_page >= 2 && $pagenavi_options['num_pages'] < $totalpage) {
				echo '<span class="extend">...</span>';
				
			}
			for($i = $start_page; $i  <= $end_page; $i++) {						
				if($i == $curpage) {
					echo '<span class="h">'.$i.'</span>';
				} else {
					echo '<a href="'.esc_url(get_pagenum_link($i)).'">'.$i.'</a>';
				}
			}
			if ($end_page < $totalpage) {
				echo '<span class="extend">...</span>';
			}
			if($curpage < $totalpage) echo '<a href="'.esc_url(get_pagenum_link($curpage+1)).'" class="next">下页</a>';
			echo '</div>';
		}
	}
}


?>
